package com.kaishengit.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.kaishengit.pojo.News;
import com.kaishengit.pojo.Project;
import com.kaishengit.pojo.User;
import com.kaishengit.service.NewsService;
import com.kaishengit.util.CreateTime;
import com.kaishengit.util.MyUUID;
import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseAction extends ActionSupport implements SessionAware{

	private static final long serialVersionUID = 1L;
	
	public static final String LIST = "list";
	
	private Map<String, Object> session;
	
	private NewsService newsService;
	private User user;
	private Project project ;
	
	@Override
	public abstract String execute() throws Exception;
	
	
	public void news(String content){
		News news2 = new News();
		
		news2.setId(MyUUID.createUUID().toString());
		news2.setCreatetime(CreateTime.createTime());
		news2.setContent(content);
		project= (Project)getFromSession("project");
		user = (User)getFromSession("user");
		newsService.save(news2,project,user);
	}
	
	
	public void sendJson(Object o) {
		Gson gson = new Gson();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=utf-8");
		try {
			PrintWriter out = response.getWriter();
			out.print(gson.toJson(o));
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public void putInSession(String key,Object value) {
		session.put(key, value);
	}
	
	public Object getFromSession(String key) {
		return session.get(key);
	}

	
	
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}


	public NewsService getNewsService() {
		return newsService;
	}

	@Autowired
	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}


	public User getUser() {
		return user;
	}


	/*public void setUser(User user) {
		this.user = user;
	}*/


	public Project getProject() {
		return project;
	}


/*	public void setProject(Project project) {
		this.project = project;
	}*/
	
	

}
